<?php

  require_once "JooS/Helper/Broker.php";

  abstract class JooS_File {
    const debugModeParamName = "server.indev";

    private static $_cacheFolder = "cache";

    private static $_appDirs = array();

    private static $_personalAppDir = null;

    private static $_params = array();

    public static function getContent($filename) {
      $path = self::getFilePath($filename);
      return $path === false ? false : file_get_contents($path);
    }
    
    public static function getFilePath($filename, $dirOnly = false) {
      $_appDirs = self::getApplicationsPaths();

      foreach ($_appDirs as $dir) {
        $realFilename = $dir . DIRECTORY_SEPARATOR . $filename;

        if (self::isDebugMode()) {
          if (file_exists($realFilename . ".indev")) {
            return $dirOnly ? $dir : $realFilename . ".indev";
          }
        }
        if (file_exists($realFilename)) {
          return $dirOnly ? $dir : $realFilename;
        }
      }
      return false;
    }

    public static function includeFile($filename) {
      $path = self::getFilePath($filename);
      if ($path !== false) {
        ob_start();
        include($path);
        return ob_get_clean();
      }
      else
        return false;
    }

    protected function __construct() {
      JooS_Helper_Broker::helperBroker()->File_InitHelper();
    }

    protected static function throwException($message, $code = 0) {
      require_once 'JooS/File/Exception.php';

      throw new JooS_File_Exception($message, $code);
    }

    public static function setApplicationDirectory($path, $name = "default", $personal = false) {
      self::$_appDirs[$name] = $path;
      if ($personal)
        self::$_personalAppDir = $path;
    }

    public static function getApplicationsPaths() {
      return self::$_appDirs;
    }

    public static function getCachePath() {
      if (self::$_personalAppDir === null)
        self::throwException("Path to local application directory has not set");
      return self::$_personalAppDir . DIRECTORY_SEPARATOR . self::$_cacheFolder;
    }

    public static function setParam($name, $value) {
      self::$_params[$name] = $value;
    }

    public static function getParam($name) {
      return isset(self::$_params[$name]) ? self::$_params[$name] : null;
    }

    public static function getParams() {
      return self::$_params;
    }

    public static function unsetParam($name) {
      if (isset(self::$_params[$name]))
        unset(self::$_params[$name]);
    }

    public static function unsetParams() {
      self::$_params = array();
    }

    public static function setDebugMode($mode = true) {
      if ($mode)
        self::setParam(self::debugModeParamName, true);
      else
        self::unsetParam(self::debugModeParamName);
    }
    
    public static function isDebugMode() {
      return self::getParam(self::debugModeParamName) ? true : false;
    }
  }

?>